import { Service } from 'egg';
import {User} from '../model/User';
import * as moment from 'moment';

class AuditingService extends Service {
    /**
     * 查询全部用户列表
     * @param param
     */
    public async searchUserList(param: any) {
        const offset = (Number(param.page) - 1) * Number(param.size);
        const limit = Number(param.size);
        let queryStr = `SELECT u.user_id,u.user_nickname,u.user_phone,u.user_sex,s.school_name,c.college_name,u.user_student_id,u.user_school_Card,u.user_auditing_status
         FROM t_user u INNER JOIN t_school s ON u.user_school_id=s.school_id AND s.school_status='1'
         INNER JOIN t_college c ON u.user_college_id=c.college_id AND c.college_status='1' WHERE u.is_delete='1'`;
        let actuleParam: any = {};
        if (param.user_phone) {
            actuleParam.user_phone = '%' + param.user_phone + '%';
            queryStr += ` AND u.user_phone LIKE :user_phone`;
        }
        if (param.user_sex) {
            actuleParam.user_sex = param.user_sex;
            queryStr += ` AND u.user_sex=:user_sex`;
        }
        if (param.user_school) {
            actuleParam.user_school = param.user_school;
            queryStr += ` AND u.user_school_id=:user_school`;
        }
        const queryCount = queryStr;
        queryStr += ` ORDER BY u.user_auditing_status ASC LIMIT ${offset},${limit}`;
        const user = await this.ctx.model.query(queryStr, {
            type: this.ctx.model.QueryTypes.SELECT,
            replacements: actuleParam
        });
        const count = await this.ctx.model.query(queryCount, {
            type: this.ctx.model.QueryTypes.SELECT,
            replacements: actuleParam
        });
        if (user.length <= 0) {
            throw new Error(JSON.stringify({ code: '2', msg: '查询无记录' }));
        }
        const result = { count: count.length, userList: user };
        return result;
    }

    /**
     * 审核用户（通过、禁止）
     * @param param 
     */
    public async auditingUser(param: any) {
        let type = Number(param.type);
        let user_auditing_status: number = -1;
        let msg=param.msg;
        if (type == 1) {
            user_auditing_status = 2;
        } else if (type == 2) {
            user_auditing_status = 3;
        }
        const resultUser = await this.ctx.model.User.update(
            { user_auditing_status },
            { where: { user_id: param.user_id } }
        );
        if (resultUser[0] <= 0) {
            throw new Error(JSON.stringify({ code: 2, msg: '审核失败' }));
        }
        if(type==2){
            const user: any = await this.ctx.model.User.findOne<User>({ attributes: ['user_openId','created_at'], where: { user_id: param.user_id } });
            const template_id = this.app.config.templateMessageId.auditingMessage;
            const user_openId = user.user_openId;
            const created_at=moment(user.created_at).format('YYYY-MM-DD HH:mm:ss');
            if(!msg){
                msg='无';
            }
            const data = {};
            data['first']={value:'您的注册审核结果如下。'};
            data['keyword1']={value:created_at};
            data['keyword2']={value:'无'};
            data['keyword3']={value:'不通过'};
            data['keyword4']={value:msg};
            data['remark']={value:'您可以点击详情向管理员反馈哦！'};
            const result = await this.ctx.service.templateService.sendWechatMsg(this.app.config.redirectUrl+'wechat/feedback', data, user_openId, template_id);
            const returnData = JSON.parse(result);
            return returnData;
        }
    }
}
export default AuditingService;